package com.dhf.framework.system.entity;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

public class RegisterUserBean {

	private String userName;	//用户名
	private String password; 	//密码
	private String repassword;//确认密码
	private String email; //邮箱
	private String checkcode;//验证码
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getRepassword() {
		return repassword;
	}
	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getCheckcode() {
		return checkcode;
	}
	public void setCheckcode(String checkcode) {
		this.checkcode = checkcode;
	}
	
	/**
	 * 存储校验不通过时给用户的错误提示信息
	 */
	private Map<String, String> errors = new HashMap<String, String>();
	
	
	public Map<String, String> getErrors() {
		return errors;
	}
	
	public void setErrors(Map<String, String> errors) {
		this.errors = errors;
	}
	
	/*
       * validate方法负责校验表单输入项
      * 表单输入项校验规则：
       *         private String userName; 用户名不能为空，并且要是3-8的字母 abcdABcd 
       *         private String userPwd; 密码不能为空，并且要是3-8的数字
       *         private String repassword; 两次密码要一致
       *         private String email; 可以为空，不为空要是一个合法的邮箱 
       */
    public boolean validate() throws Exception {
         boolean isOk = true;
 
         if(StringUtils.isEmpty(this.userName)){
             isOk = false;
             errors.put("key","userName");
             errors.put("info", "用户名不能为空！！");
         } else {
              if (!this.userName.matches("[a-zA-Z]{3,8}")) {
                  isOk = false;
                  errors.put("key","userName");
                  errors.put("info", "用户名必须是3-8位的字母！！");
              }
             
          }
 
          if (StringUtils.isEmpty(this.password)){
             isOk = false;
             errors.put("key","password");
             errors.put("info", "密码不能为空！！");
          } else {
              if (!this.password.matches("\\d{3,8}")) {
                 isOk = false;
                 errors.put("key","password");
                 errors.put("info", "密码必须是3-8位的数字！！");
              }
          }
  
          // private String password2; 两次密码要一致
          if (StringUtils.isNotEmpty(this.repassword)) {
             if (!this.repassword.equals(this.password)) {
                  isOk = false;
                  errors.put("key","repassword");
                  errors.put("info", "两次密码不一致！！");
             }
          }
  
          // private String email; 可以为空，不为空要是一个合法的邮箱
          if (StringUtils.isNotEmpty(email)) {
        	  Pattern pattern = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
              if (!pattern.matcher(this.email).matches()) {
                 isOk = false;
                 errors.put("key","email");
                 errors.put("info", "邮箱不是一个合法邮箱！！");
             }
          }
 
         return isOk;
	}
	
}
